package work;

import work.config.Constant;

import java.io.*;
import java.sql.*;
import java.util.regex.Pattern;

public class JDBC {

    private static Connection conn;

    static {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            try {
                conn = DriverManager.getConnection("jdbc:mysql://192.168.2.251:3306/mdm?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8", "glsh", "glsh"); //创建连接
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws IOException, SQLException {

        Pattern pattern = Pattern.compile("");

        File file = new File(Constant.work, "无法执行的sql.txt");

        if (!file.exists()) {
            file.createNewFile();
        }

        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));

        BufferedReader reader = new BufferedReader(new FileReader(new File("C:\\Users\\dinglei\\Desktop\\新建文本文档(1).txt")));

        Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

        int i = 0;

        String line;

        while ((line = reader.readLine()) != null) {
            i++;
            try {
                stmt.execute(line);
            } catch (Exception e) {
                // bufferedWriter.write("第" + i + "行错误:     " + line + "\n");

                if (!e.getMessage().contains("Duplicate entry ")) {
                    System.out.println("第" + i + "行错误:     " + line);
                    System.out.println(e.getMessage());
                }

            }
        }

        bufferedWriter.flush();

        bufferedWriter.close();


    }

}
